<?php
/**
 * Description of UtentiController
 *
 * @author luigi.pastore
 * @created  4-mag-2011
 */ 

class UtentiController extends Classes_Controller
{
    public function init()
    {
	$this->model_utenti	= new Classes_Users();
	$this->model_groups	= new Classes_UserGroups;

	$this->layout->groups = $this->model_groups->fetchAll();	
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'utenti/index' );
    }
    
    public function archivioAction()
    {
	$this->layout->users = $this->model_utenti->fetchAll();
	$this->layout->content = array( 'script' => 'utenti/archivio' );
    }

    public function nuovoAction()
    {
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;
	    
	    // Validiamo server-side...
	    if( !filter_var( $_POST['user_firstname'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[a-zA-Z \']+$/' ) ) ) OR !filter_var( $_POST['user_lastname'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[a-zA-Z \']+$/' ) ) ) )
		$errorMsg['user_firstname'] = sprintf( ERR_FORMAT, 'Nome o Cognome' );

	    if( !filter_var( $_POST['user_email'], FILTER_VALIDATE_EMAIL ) )
		$errorMsg['user_email'] = sprintf( ERR_FORMAT, 'Email' );
	    
	    if( count( $this->model_utenti->fetchByEmail( $_POST['user_email'] ) ) > 0 )
		$errorMsg['user_email'] = sprintf( ERR_DUPLICATE, 'Email' );

	    if( !filter_var( $_POST['user_password'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[0-9a-zA-Z.]{6,}$/i' ) ) ) )
		$errorMsg['user_email'] = ERR_PASSWORD;
		    
	    if ( $_POST['user_password'] !== $_POST['password_again'] )
		$errorMsg['user_password'] = ERR_PASSWORD_MATCH;

	    if( !$errorMsg )
	    {
		# Mapping...
		$data = array(
		    'user_firstname' => $_POST['user_firstname'],
		    'user_lastname' => $_POST['user_lastname'],
		    'user_password' => $_POST['user_password'],
		    'user_email' => $_POST['user_email'],
		    'user_group_id' => $_POST['user_group_id'],
		    'user_createdby' => $this->application->auth->user['user_id']
		);
		
		$model = new Classes_Users( $data );

		if( is_numeric( $user_id = $model->save()->user_id ) )
		{
		    // Rimanda alla pagina di conferma...
		    $this->layout->forward = 'utenti/';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Inserimento Utente' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'utenti/nuovo' );
    }
    
    public function editAction()
    {
	// Edit
	if( !is_null( $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $user = $this->model_utenti->fetchById( (int) $this->getParam( 'id' ) );

	    // Se non c'è lancia l'errore
	    if( !is_array( $user ) )
	    {
		trigger_error( 'Errore Edit Utente', E_USER_WARNING );
	    }
	    else
	    {
		# Assegniamo i dati al layout
		$this->layout->data = $user;
	    }
	}
	
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;
	    
	    // Validiamo server-side...
	    if( !filter_var( $_POST['user_firstname'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[a-zA-Z \']+$/' ) ) ) OR !filter_var( $_POST['user_lastname'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[a-zA-Z \']+$/' ) ) ) )
		$errorMsg['user_firstname'] = sprintf( ERR_FORMAT, 'Nome o Cognome' );

	    if( !filter_var( $_POST['user_email'], FILTER_VALIDATE_EMAIL ) )
		$errorMsg['user_email'] = sprintf( ERR_FORMAT, 'Email' );
	    
	    $users = $this->model_utenti->fetchByEmail( $_POST['user_email'] );

	    if( count( $users ) > 0 )
		foreach( $users AS $user )
		    if( $user['user_id'] != $_POST['user_id'] )
			$errorMsg['user_email'] = sprintf( ERR_DUPLICATE, 'Email' );

	    if( $_POST['user_password'] != '' )
	    {
		if( !filter_var( $_POST['user_password'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[0-9a-zA-Z.]{6,}$/i' ) ) ) )
		    $errorMsg['user_email'] = ERR_PASSWORD;
		    
		if ( $_POST['user_password'] !== $_POST['password_again'] )
		    $errorMsg['user_password'] = ERR_PASSWORD_MATCH;
	    }

	    if( !$errorMsg )
	    {
		# Mapping...
		$data = array(
		    'user_id' => $_POST['user_id'],
		    'user_firstname' => $_POST['user_firstname'],
		    'user_lastname' => $_POST['user_lastname'],
		    'user_password' => $_POST['user_password'] ? $_POST['user_password'] : null,
		    'user_email' => $_POST['user_email'],
		    'user_group_id' => $_POST['user_group_id'],
		    'user_modifiedby' => $this->application->auth->user['user_id']
		);
		
		$model = new Classes_Users( $data );

		if( is_numeric( $user_id = $model->save()->user_id ) )
		{
		    // Rimanda alla pagina di conferma...
		    $this->layout->forward = 'utenti/';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Modifica Utente' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'utenti/edit' );	
    }
    
    public function checkAction()
    {
	$this->layout->disableLayout( true );

	switch( $_GET['f'] )
	{
	    case 'email':

		/**
		 * Controlla che la mail non esista già
		 * Se $_POST['user_id'] != '', stiamo editando
		 * e quindi torniamo false solo se esiste un utente con la stessa
		 * email ma user_id diverso.
		 */
		$users = $this->model_utenti->fetchByEmail( $_POST['user_email'] );
		
		if( count( $users ) > 0 )
		    if( is_null( $_POST['user_id'] ) )
			exit( 'false' );
		    else
			foreach( $users AS $user )
			    if( $user['user_id'] != $_POST['user_id'] )
				exit( 'false' );

		break;

	    case 'name':
		if( !filter_var( $_POST['name'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[a-zA-Z \']+$/' ) ) ) )
		    exit( 'false' );
		
		break;

	    default:
		exit( 'false' );
		break;
	}
	
	# Se è tutto ok...
	exit( 'true' );	
    }
}
